package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.DictItemDao;
import com.hxkj.domain.enums.DictItemExceptionEnum;
import com.hxkj.domain.po.DictItem;
import com.hxkj.service.DictItemService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 二 级字典 实现类
 * <p>
 * Created by zzx on 2018/5/19.
 */
@Service
public class DictItemServiceImpl implements DictItemService {


    @Autowired
    private DictItemDao dictItemDao;

    @Override
    public PageInfo<DictItem> getDictItemList(DictItem dictItem, PageTo pageParam) throws Exception {
        Page<DictItem> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        dictItemDao.get(dictItem);
        PageInfo<DictItem> pageInfo = new PageInfo<>(page);
        return pageInfo;

    }


    @Override
    public ResultDataStruct getDictItemFromCache(DictItem dictItem) throws Exception {
        ResultDataStruct<DictItem> resul = new ResultDataStruct();
        DictItem dict;
        try {
            //通过 id 查询二级字典信息
            dict = dictItemDao.get(dictItem.getDictItemId());
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTTYPE_GET_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTTYPE_GET_ERROR.getMsg());
            return resul;
        }
        resul.setData(dict);

        return resul;
    }

    @Override
    public ResultMessageStruct updateDictItem(DictItem dictItem) {
        ResultMessageStruct resul = new ResultMessageStruct();
        try {
            //查询二级字典名称是否重名 ，允许其他一级子类与本身一级子类重名！   同一级别类不能重名
            List<DictItem> ItemName = dictItemDao.selectItemName(dictItem);
            if (ItemName.size() > 0) {
                resul.setCode(DictItemExceptionEnum.DICTTYP_NAME_ERROR.getCode());
                resul.setMessage(DictItemExceptionEnum.DICTTYP_NAME_ERROR.getMsg());
                return resul;
            }
            dictItemDao.update(dictItem);
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTTYP_MODIFY_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTTYP_MODIFY_ERROR.getMsg());
            return resul;
        }
        return resul;
    }

    @Override
    public ResultMessageStruct deleteDictItem(DictItem dictItem) {
        ResultMessageStruct resul = new ResultMessageStruct();

        try {
            //删除二级字典
            dictItemDao.delete(dictItem.getDictItemId());
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTTYP_DELETE_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTTYP_DELETE_ERROR.getMsg());
            return resul;
        }
        return resul;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct saveDictItem(DictItem dictItem) {
        ResultMessageStruct resul = new ResultMessageStruct();
        try {
            // 查询二级字典名称是否重名 ，允许其他一级子类与本身一级子类重名！   同一级别类不能重名
            List<DictItem> ItemName = dictItemDao.selectItemName(dictItem);
            if (ItemName.size() > 0) {
                resul.setCode(DictItemExceptionEnum.DICTTYP_NAME_ERROR.getCode());
                resul.setMessage(DictItemExceptionEnum.DICTTYP_NAME_ERROR.getMsg());
                return resul;
            }
            dictItemDao.save(dictItem);

        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTTYP_SAVE_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTTYP_SAVE_ERROR.getMsg());
            return resul;
        }
        return resul;
    }


    @Override
    public DictItem getDictItemRefresh(Long dictItemId) throws Exception {
        return dictItemDao.get(dictItemId);

    }

    @Override
    public ResultMessageStruct modifyDictItem(DictItem dictItem) {
        ResultMessageStruct resul = new ResultMessageStruct();

        try {
            // 禁用二级字典
            dictItemDao.update(dictItem);
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTTYP_DEL_DAG_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTTYP_DEL_DAG_ERROR.getMsg());
            return resul;
        }
        return resul;
    }

    @Override
    public ResultDataStruct comboBox(DictItem dictItem) throws Exception {
        ResultDataStruct resul = new ResultDataStruct();
        List<DictItem> dictItemList = new ArrayList<>();
        try {
            dictItemList = dictItemDao.selectComboBox(dictItem);
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTTYP_COMBOX_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTTYP_COMBOX_ERROR.getMsg());
            return resul;
        }
        resul.setData(dictItemList);

        return resul;
    }

    @Override
    public ResultDataStruct<List<DictItem>> getDictItemByDictType(DictItem dictItem) throws Exception {
        ResultDataStruct<List<DictItem>> resul = new ResultDataStruct<>();
        List<DictItem> dict;
        try {
            dict = dictItemDao.get(dictItem);
        } catch (Exception e) {
            resul.setCode(DictItemExceptionEnum.DICTTYPE_GET_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTTYPE_GET_ERROR.getMsg());
            return resul;
        }
        resul.setData(dict);
        resul.setCode(200);
        return resul;
    }

}
